package com.zzy.forum.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.forum.mapper.*;
import com.zzy.forum.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import sun.nio.cs.US_ASCII;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class BlogsController {
    @Autowired
    private BlogsMapper blogsMapper;
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserBlogsMapper userBlogsMapper;

    //详细页面 detail
    @GetMapping("/getdetail/{blogId}")
    public String getDetail(Model model,@PathVariable("blogId") Integer blogId,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }

        Blogs blog = blogsMapper.selectById(blogId);
        model.addAttribute("blod",blog);

        QueryWrapper<UserBlogs> userBlogsQueryWrapper = new QueryWrapper<>();
        userBlogsQueryWrapper.eq("user_id",userId);
        userBlogsQueryWrapper.eq("blog_id",blogId);
        UserBlogs userBlogs = userBlogsMapper.selectOne(userBlogsQueryWrapper);
        if (userBlogs ==null){
            model.addAttribute("userblogs",true);
        }else {
            model.addAttribute("userblogs",false);
        }


        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("blogs_id",blog.getId());
        List<Comment> comments = commentMapper.selectList(commentQueryWrapper);
        model.addAttribute("comments",comments);

        QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("user_id",userId);
        friendQueryWrapper.eq("other_id",blog.getUserId());
        Friend friend = friendMapper.selectOne(friendQueryWrapper);
        if (friend == null){
            model.addAttribute("friend",true);
        }else {
            model.addAttribute("friend",false);
        }
        return "blogs/detail";
    }

    //好友页面  friend
    @GetMapping("/getfriend")
    public String getFriend(Model model, HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        HashMap<String ,String> map = new HashMap<>();
        Integer userId = null;
        for (Cookie cookie : cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("user_id",userId);
        List<Friend> friends1 = friendMapper.selectList(friendQueryWrapper);
        List<List<Blogs>> blogs = new ArrayList<>();
        List<User> users = new ArrayList<>();
        for (Friend friend : friends1){
            User user = userMapper.selectById(friend.getOtherId());
            users.add(user);
            QueryWrapper<Blogs> blogsQueryWrapper = new QueryWrapper<>();
            blogsQueryWrapper.eq("user_id",friend.getOtherId());
            List<Blogs> blogsList = blogsMapper.selectList(blogsQueryWrapper);
            blogs.add(blogsList);
        }

        List<User> like = userMapper.getLike();

        model.addAttribute("userLike",like);
        model.addAttribute("blogs",blogs);
        model.addAttribute("users",users);
        return "blogs/friend";
    }

    //删除好友
    @GetMapping("/delfriend/{otherId}")
    public String delFriend(@PathVariable("otherId") String otherId,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("user_id",userId);
        friendQueryWrapper.eq("other_id",otherId);
        Friend friend = friendMapper.selectOne(friendQueryWrapper);
        friendMapper.deleteById(friend.getId());
        return "forward:/getfriend";
    }

    //发布动态页面 message
    @GetMapping("/getmessage")
    public String getMessage(){
        return "blogs/message";
    }
    @PostMapping("/blogs/message")
    public String postMessage(Model model,Blogs blogs,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        User user = userMapper.selectById(userId);
        user.setCount(user.getCount()+1);
        userMapper.updateById(user);
        blogs.setUserId(userId);
        blogs.setIsLike(0);
        blogs.setIsDel(0);
        blogsMapper.insert(blogs);
        model.addAttribute("addblogs","添加成功");
        return "blogs/message";
    }

    // 我的博客页面 other
    @GetMapping("/getother")
    public String getOther(Model model,Blogs blogs,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        QueryWrapper<Blogs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("id");
        List<Blogs> blogs1 = blogsMapper.selectList(queryWrapper);
        model.addAttribute("blogs",blogs1);
        return "blogs/other";
    }
    @GetMapping("/blogs/del/{blogId}")
    public String delBlog(@PathVariable("blogId") Integer blogId){
        blogsMapper.deleteById(blogId);
        return "forward:/getother";
    }
    //搜索用户的blogs
    @PostMapping("/selectBlogs")
    public String selectBlogs(String username,Model model){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        QueryWrapper<Blogs> blogsQueryWrapper = new QueryWrapper<>();
        blogsQueryWrapper.eq("user_id",user.getId());
        List<Blogs> blogs = blogsMapper.selectList(blogsQueryWrapper);
        model.addAttribute("blogs",blogs);
        return "blogs/friend";
    }

    //添加评论
    @GetMapping("/blogs/comment/{blogsId}")
    public String addComment(@PathVariable("blogsId") Integer blogsId,Model model,HttpServletRequest request,Comment comment){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        User user = userMapper.selectById(userId);
        comment.setUsername(user.getUsername());
//        comment.setBlogsId(blogsId);
        commentMapper.insert(comment);
        return "forward:/getdetail/"+blogsId;
    }
    @GetMapping("/like/{BlogId}")
    public String like(@PathVariable("BlogId") Integer BlogId,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        Blogs blog = blogsMapper.selectById(BlogId);
        QueryWrapper<UserBlogs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("blog_id",BlogId);
        UserBlogs userBlogs1 = userBlogsMapper.selectOne(queryWrapper);

        if (userBlogs1 == null){
            UserBlogs userBlogs = new UserBlogs();
            userBlogs.setUserId(userId);
            userBlogs.setBlogId(BlogId);
            blog.setIsLike(blog.getIsLike()+1);
            blogsMapper.updateById(blog);
            userBlogs.setIsActive(false);
            userBlogsMapper.insert(userBlogs);
        }else {
            if (userBlogs1.getIsActive()){
                blog.setIsLike(blog.getIsLike()+1);
                blogsMapper.updateById(blog);
                userBlogs1.setIsActive(false);
            }else {
                blog.setIsLike(blog.getIsLike()-1);
                blogsMapper.updateById(blog);
                userBlogs1.setIsActive(true);
            }
            userBlogsMapper.updateById(userBlogs1);
        }
        return "forward:/";
    }
    @GetMapping("/addfriend/{otherId}")
    public String addFriend(@PathVariable("otherId") Integer otherId,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setOtherId(otherId);
        friendMapper.insert(friend);
        return "forward:/";
    }
    @GetMapping("/friendlike/{blogId}")
    public String friendlike(@PathVariable("blogId") Integer blogId,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer userId = null;
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("user_id")){
                userId = new Integer(cookie.getValue());
                break;
            }
        }
        QueryWrapper<UserBlogs> userBlogsQueryWrapper = new QueryWrapper<>();
        userBlogsQueryWrapper.eq("user_id",userId);
        userBlogsQueryWrapper.eq("blog_id",blogId);
        UserBlogs userBlogs = userBlogsMapper.selectOne(userBlogsQueryWrapper);

        Blogs blogs = blogsMapper.selectById(blogId);

        if (userBlogs.getIsActive()){
            blogs.setIsLike(blogs.getIsLike()+1);
            blogsMapper.updateById(blogs);
            userBlogs.setIsActive(false);
        }else {
            blogs.setIsLike(blogs.getIsLike()-1);
            blogsMapper.updateById(blogs);
            userBlogs.setIsActive(true);
        }
        userBlogsMapper.updateById(userBlogs);
        return "forward:/getfriend";

    }
    @GetMapping("/friendblogs/{userId}")
    public String friendLike(@PathVariable("userId") Integer userId,Model model){
        QueryWrapper<Blogs> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id",userId);
        List<Blogs> blogsList = blogsMapper.selectList(userQueryWrapper);
        model.addAttribute("blogsList",blogsList);
        return "blogs/other";
    }

    @GetMapping("/blogs/updatepage/{blogId}")
    public String updatepage(@PathVariable("blogId") Integer blogId,Model model){
        model.addAttribute("blogid",blogId);
        Blogs blogs1 = blogsMapper.selectById(blogId);
        model.addAttribute("blog",blogs1);
        return "blogs/messagebak";
    }
    @PostMapping("/blogs/update/{blogId}")
    public String update(@PathVariable("blogId") Integer blogId,Model model,String title ,String intro,String content){
        model.addAttribute("blogid",blogId);
        Blogs blogs1 = blogsMapper.selectById(blogId);
        blogs1.setTitle(title);
        blogs1.setIntro(intro);
        blogs1.setContent(content);
        blogsMapper.updateById(blogs1);
        model.addAttribute("addblogs","修改成功");
        model.addAttribute("blog",blogs1);
        return "blogs/messagebak";
    }
}
